package com.suhe.stock.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.map.MapUtil;
import com.suhe.stock.entity.StockKLine;
import com.suhe.stock.http.StockCodeListHttp;
import com.suhe.stock.http.StockKLineHttp;
import com.suhe.stock.service.StockKLineService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 股票api
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("stock")
public class StockController {

    private final StockKLineService stockKLineService;

    /**
     * 同步某只股票日线数据
     *
     * @param code
     * @throws IOException
     */
    @GetMapping("kline/sync")
    public void klineSync(@RequestParam("code") String code) throws IOException {
        StockCodeListHttp.getData().stream().filter(p -> code.equals(p.split("\\.")[1])).findFirst().ifPresent(stockCode -> {
            List<StockKLine> stockKLines = null;
            try {
                stockKLines = StockKLineHttp.getData(stockCode);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            stockKLineService.batchReplaceInto(stockKLines);
        });
    }

    /**
     * 获取某只股票的日线数据
     *
     * @param code 股票代码
     * @return
     */
    @GetMapping("klines")
    public Map kLines(@RequestParam("code") String code) {
        List<StockKLine> stockKLines = stockKLineService.findByCodeOrderByDate(code);
        List<String> dates = stockKLines.stream().map(p -> LocalDateTimeUtil.format(p.getDate(), "yy/MM/dd")).collect(Collectors.toList());
        List<Double[]> klines = stockKLines.stream().map(p -> new Double[]{p.getOpen(), p.getClose(), p.getLow(), p.getHigh()}).collect(Collectors.toList());
        List<Double> turnovers = stockKLines.stream().map(StockKLine::getTurnover).collect(Collectors.toList());
        List<Double> ups = stockKLines.stream().map(StockKLine::getUp).collect(Collectors.toList());
        List<Double> upRates = stockKLines.stream().map(StockKLine::getUpRate).collect(Collectors.toList());
        List<Double> ma5s = stockKLines.stream().map(StockKLine::getMa5).collect(Collectors.toList());
        List<Double> ma10s = stockKLines.stream().map(StockKLine::getMa10).collect(Collectors.toList());
        List<Double> ma20s = stockKLines.stream().map(StockKLine::getMa20).collect(Collectors.toList());
        List<Double> ma30s = stockKLines.stream().map(StockKLine::getMa30).collect(Collectors.toList());
        List<Double> ma60s = stockKLines.stream().map(StockKLine::getMa60).collect(Collectors.toList());
        List<Double> ma120s = stockKLines.stream().map(StockKLine::getMa120).collect(Collectors.toList());
        return MapUtil.builder()
                .put("name", CollectionUtil.isEmpty(stockKLines) ? "" : stockKLines.get(0).getName())
                .put("code", CollectionUtil.isEmpty(stockKLines) ? "" : stockKLines.get(0).getCode())
                .put("date", dates)
                .put("kline", klines)
                .put("turnover", turnovers)
                .put("up", ups)
                .put("upRate", upRates)
                .put("ma5", ma5s)
                .put("ma10", ma10s)
                .put("ma20", ma20s)
                .put("ma30", ma30s)
                .put("ma60", ma60s)
                .put("ma120", ma120s).build();
    }
}
